
$.get('http://localhost:3000/card', function (res) {
    // console.log(res[0].data);
    res.forEach(a => {
        // console.log(a.uid,user);
        if (a.uid == user) {
            console.log(a.data);
            let arr = JSON.parse(a.data)
            console.log(arr);
            $('.cart span').html(arr.length)
        }
    })
}, 'json')
//划过
$('.div').hover(function () {
    //$('.div').slideDown(1000)
    $('.span').css({

        transform: 'rotate(180deg)'
    })
    $('.slide_down').css({
        display: 'flex'
    })

}, function () {
    $('.slide_down').hide()
    $('.span').css({

        transform: 'rotate(360deg)'
    })

})
$('#nav li').hover(function () {
    $(this).css({
        borderBottom: '4px solid yellow',
        color: 'white',
    })
}, function () {
    $(this).css({
        border: 'none',
        color: 'rgb(170, 170, 170)',
    })
})


var user = localStorage.getItem('uid')
var cartarr = [];
var id;
$.get('http://localhost:3000/card', function (arr) {
    console.log(arr);
    arr.forEach(v => {
        if (v.uid == user) {
            id = v.id
            let cart = JSON.parse(v.data)
            console.log(cart);
            cartarr = cart
            cart.forEach(arr => {
                $('#main').append(`  
        <div>
            <input type="checkbox" class="single">
            <p><img src="${arr.pimg}"></p>
            <h3>${arr.pitle}</h3>
            <span class="odd-price">￥${arr.pprice}</span>
            <div class="button">
            <button class="down-btn"  pid="${arr.pid}" price="${arr.pprice}">-</button>
            <button>${arr.pnum}</button>
            <button class="add-btn" pid="${arr.pid}" price="${arr.pprice}">+</button> 
        </div>
        <span class="even-price">￥
        ${arr.pprice * arr.pnum}
        </span>
        <p class="del" pid="${arr.pid}">删除</p>
        </div>`)



            })


        }
    })


    console.log(cartarr);
    console.log($('.del').parent());
    $('.del').click(function () {
        console.log($(this));
        let pid = $(this).attr('pid');
        console.log(pid);
        console.log(cartarr);
        console.log(id);
        var that = $(this)
        cartarr.forEach((v, i) => {
            console.log(v, i);
            if (v.pid == pid) {
                console.log(cartarr);
                cartarr.splice(i, 1)
                let a = JSON.stringify(cartarr)
                console.log(cartarr);
                let url = `http://localhost:3000/card/${id}`

                $.ajax({
                    url: url,
                    data: { data: a },
                    type: 'patch',
                    success(res) {
                        alert('删除成功')

                        that.parent().remove();





                        $.get('http://localhost:3000/card', function (res) {
                            // console.log(res[0].data);
                            res.forEach(a => {
                                // console.log(a.uid,user);
                                if (a.uid == user) {
                                    console.log(a.data);
                                    let arr = JSON.parse(a.data)
                                    console.log(arr);
                                    $('.cart span').html(arr.length)
                                }
                            })

                        }, 'json')



                    }
                })
            }
        })




    })
    let index;
    //点击增添商品
    $('.add-btn').click(function () {
        $(this).prev().html($(this).prev().html() - 0 + 1)
        let pid = $(this).attr('pid');
        let price = $(this).attr('price');
        console.log(id, price);
        let a = $(this)

        console.log(cartarr);

        cartarr.forEach((v, i) => {
            console.log(v, i);
            if (v.pid == pid) {
                let pnum = parseInt($(this).prev().html())
                v.pnum = pnum
                index = i
            }
        });
        console.log(cartarr);
        let cart = JSON.stringify(cartarr)
        let url = `http://localhost:3000/card/${id}`
        $.ajax({
            url,
            data: { data: cart },
            type: 'patch',
            success() {
                var s = a.parent().next().html()
                let res = s.replace(/￥/ig, "")
                a.parent().next().html('￥' + (res - 0 + (price - 0)))
            }
        })
        $('.single').eq(index).trigger('click')
        $('.single').eq(index).trigger('click')
    })
    $('.down-btn').click(function () {
        if ($(this).next().html() == 1) {
            $(this).next().html(1)
        } else {
            let price = $(this).attr('price');
            $(this).next().html($(this).next().html() - 1)
            let pid = $(this).attr('pid');
            let b = $(this)


            cartarr.forEach((v, i) => {
                console.log(v, i);
                if (v.pid == pid) {
                    let pnum = parseInt($(this).next().html())
                    v.pnum = pnum
                    index = i
                }
            });
            let cart = JSON.stringify(cartarr)
            let url = `http://localhost:3000/card/${id}`
            $.ajax({
                url,
                data: { data: cart },
                type: 'patch',
                success() {
                    var s = b.parent().next().html()
                    let res = s.replace(/￥/ig, "")
                    b.parent().next().html('￥' + (res - 0 - (price - 0)))
                }
            })

        }
        $('.single').eq(index).trigger('click')
        $('.single').eq(index).trigger('click')
    })
    choose()

})
















function choose() {
    //全选
    $('.allsingle').click(function () {
        let allprice = 0, allnum = 0
        $('.single').prop('checked', $('.allsingle').prop('checked'))

        if ($('.single').prop('checked')) {
            cartarr.forEach(v => {
                allnum += (v.pnum - 0)
                allprice += (v.pprice - 0) * (v.pnum - 0)
            });

            $('.allprice').html(allprice + '元')
            $('.allnum').html(allnum)
        } else {
            $('.allprice').html(0 + '.00元')
            $('.allnum').html(0)
        }
    })
    //单选
    $('.single').click(function () {
        let allprice = 0, allnum = 0
        console.log($('.single').prop('checked'));
        let flag = true
        $('.single').each((i, v) => {
            if (v.checked == false) {
                flag = false
            } else {
                allprice += (cartarr[i].pprice - 0) * (cartarr[i].pnum - 0)
                allnum += (cartarr[i].pnum - 0)

            }
        })
        $('.allsingle').prop('checked', flag)

        $('.allprice').html(allprice + '元')
        $('.allnum').html(allnum)

    })
}

